package com.jt.assistant.ui

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.afollestad.materialdialogs.list.listItems
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ToastUtils
import com.jt.assistant.R
import com.jt.assistant.adapter.SendPlanAdapter
import com.jt.assistant.base.GlobalConfig
import com.jt.assistant.bean.DeliveryBean
import com.jt.assistant.bean.Param
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_car_receive.*

/**
 * 客户送仓交接单
 */
class CustomerDeliveryActivity : BaseActivity<HomeViewModel>() {
    private val mAdapter by lazy {
        SendPlanAdapter(
            ArrayList<DeliveryBean>()
        )
    }

    private var mDay = "1"

    override fun layoutId(): Int = R.layout.activity_customer_delivery

    override fun initView(savedInstanceState: Bundle?) {
        with(rv_carReceiver) {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    override fun initData() {}

    override fun initListener() {

        tv_car_time.setOnClickListener {
            showTimeListDialog()
        }

        set_carReceive.setOnScanListener {
            if (it.isEmpty()) {
                ToastUtils.showShort("请输入客户名称")
                return@setOnScanListener
            }
            val params = Param::class.java.newInstance()
            params.customerName = it
            params.day = mDay
            viewModel.selfSendPlan(params)
        }

        viewModel.mGetSelfSendPlan.observe(this, {
            log(it)
            if (it.isSuccess()) {
                if (it.data.isEmpty()) ToastUtils.showShort("未搜索到数据")
                mAdapter.data.clear()
                mAdapter.data.addAll(it.data)
                mAdapter.notifyDataSetChanged()
            }
        })


        mAdapter.setOnItemClickListener { adapter, view, position ->

            startActivity(
                Intent(
                    this@CustomerDeliveryActivity,
                    OrderSelectionActivity::class.java
                ).apply {
                    putExtra(
                        "waybillList",
                        GsonUtils.toJson(mAdapter.data[position].waybillLists)
                    )
                    putExtra(GlobalConfig.RELATION_ID, mAdapter.data[position].id)
                    //派车类型 1：提货   2：出货(这里再提货交接单 生成电子交接单时需要用到 用来区分交接单类型 直发和提货回仓)'
                    putExtra(GlobalConfig.ATYPE, "2")
                    putExtra(GlobalConfig.LICENSE_NAME, mAdapter.data[position].customerName)
                })

            set_carReceive.clearData()
            mAdapter.data.clear()
            mAdapter.notifyDataSetChanged()
        }
    }


    private fun showTimeListDialog() {
        val list = listOf(
            "近一天",
            "近三天",
            "近七天",
        )

        MaterialDialog(this).show {
            listItems(items = list) { _, index, text ->
                this@CustomerDeliveryActivity.tv_car_time.text = text
                mDay = when (index) {
                    0 -> {
                        "1"
                    }
                    1 -> {
                        "3"
                    }
                    else -> {
                        "7"
                    }
                }

                if (this@CustomerDeliveryActivity.set_carReceive.string.isNotEmpty()){
                    val params = Param::class.java.newInstance()
                    params.customerName = this@CustomerDeliveryActivity.set_carReceive.string
                    params.day = mDay
                    viewModel.selfSendPlan(params)
                }
            }
            lifecycleOwner(this@CustomerDeliveryActivity)
        }
    }
}